#pragma once
#include <cstdint>
#include <string>
#include <map>
#include "Singleton.h"
#include "def.h"
#include <unordered_map>


enum EKBEvent_Error
{
    KB_ERR_NONE = 0,
    KB_ERR_INVALID_PARAM = 1,
    KB_ERR_INVALID_UID = 2,
    KB_ERR_INVALID_ORDER = 3,
    KB_ERR_INNER_ERROR = 4,

    KB_ERR_NOTENOUGHBALANCE = 5,        // 金额不足
    KB_ERR_INNER_DB_ERROR = 6,          // 数据库错误
    KB_ERR_ALREADY_MATCHREG = 7,        // 比赛已经报名了
    KB_ERR_ALREADY_MATCHUNREG = 8,      // 比赛已经取消报名了
    KB_ERR_ALREADY_MATCHSETTLE = 9,     // 比赛已经结算了
    KB_ERR_NOT_MATCHREG = 10,           // 比赛未报名
    KB_ERR_NOT_MATCHORDER = 11,         // 比赛订单不一致
};

struct MatchOrderData
{
    std::uint32_t uid;
    std::uint32_t game;                     // 游戏id
    std::uint32_t matchtype;                // 赛事id
    std::uint32_t agencyid;                 // 代理商账号
    std::string currency;                   // 币种
    std::string regid;                      // 报名id
    std::string unregid;                    // 取消报名id
    std::string settlementid;               // 结算id
    std::int64_t fee;                       // 报名金额
    std::int64_t settlement;                // 结算金额

    MatchOrderData();
	MatchOrderData& operator = (const MatchOrderData& that);
	MatchOrderData& operator = (MatchOrderData&& that);

    std::string ToString() const;
};


class MatchOrderMgr
{
public:
	MatchOrderMgr();
    virtual ~MatchOrderMgr();

    typedef std::unordered_map<std::uint32_t, MatchOrderData>       MATCHORDERMAP;
    typedef std::map<std::uint64_t, MATCHORDERMAP>                  MATCHORDERMGRMAP;
public:
    static MatchOrderMgr* Instance()
	{
		return CSingleton<MatchOrderMgr>::Instance();
	}

public:
	std::int32_t Get(const std::uint32_t uid, std::uint64_t matchid, std::uint64_t matchtime, const std::string& gamePlat, MatchOrderData& order_data);
	
	void CreateMatch(const std::uint32_t uid, std::uint64_t matchid, std::uint64_t matchtime, const std::string& gamePlat, MatchOrderData&& order_data);
	void MatchReg(const std::uint32_t uid, std::uint64_t matchid, std::uint64_t matchtime, const std::string& gamePlat, const std::string& regid, std::int64_t fee);
	void MatchUnReg(const std::uint32_t uid, std::uint64_t matchid, std::uint64_t matchtime, const std::string& gamePlat, const std::string& unregid);
	void MatchSettlement(const std::uint32_t uid, std::uint64_t matchid, std::uint64_t matchtime, const std::string& gamePlat, const std::string& settlementid, std::int64_t settlement);

public:
    EKBEvent_Error CheckMatchReg(std::uint32_t uid, const std::string& gamePlat, std::uint64_t matchid, const std::string& order, std::int64_t fee, std::int32_t matchtime, std::int32_t &firstReg);
    EKBEvent_Error CheckMatchUnReg(std::uint32_t uid, const std::string& gamePlat, std::uint64_t matchid, const std::string& order, std::int32_t matchtime, std::int64_t& fee);
    EKBEvent_Error CheckMatchSettlement(std::uint32_t uid, const std::string& gamePlat, std::uint64_t matchid, const std::string& order, std::int64_t settlement, std::int32_t matchtime);

private:
    MATCHORDERMGRMAP      m_MatchOrders;
};












